# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Maintainer: Naveen M K <naven@syrusdark.website>

_realname=distlib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.3.6
pkgrel=1
pkgdesc="Low-level components of distutils2/packaging (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/pypa/distlib"
license=('FSF')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' 'strip' '!debug')
source=("https://files.pythonhosted.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "003-launcher-secure-api-shim.patch")
sha256sums=('14bad2d9b04d3a36127ac97f30b12a19268f211063d8f8ee4f47108896e11b46'
            'f90de01cd05cf901e9ac375e5f92727b83cb90ce3847d6ac0a18e504cd3793ca')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  cd "python-build-${MSYSTEM}"
  patch -Np1 -i "${srcdir}/003-launcher-secure-api-shim.patch"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  rm -f distlib/{w,t}{32,64}{,-arm}.exe
  local _gui_cflags="-DWIN32 -DNDEBUG -D_WINDOWS -mwindows -O2"
  local _cli_cflags="-DWIN32 -DNDEBUG -D_CONSOLE -O2"
  local _gui_lflags="-lshlwapi"
  local _cli_lflags="-lshlwapi"
  if check_option "strip" "y"; then
    _gui_cflags+=" -s"
    _cli_cflags+=" -s"
  fi

  windres --input PC/launcher.rc --output PC/resources.res --output-format=coff

  case "${MINGW_CHOST}" in
  i686-w64-mingw32)
    cc $_cli_cflags -o distlib/t32.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w32.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  x86_64-w64-mingw32)
    cc $_cli_cflags -o distlib/t64.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w64.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  aarch64-w64-mingw32)
    cc $_cli_cflags -o distlib/t64-arm.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w64-arm.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  *)
    echo "Unsupported CHOST ${MINGW_CHOST}" && false
    ;;
  esac

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  msg "Python test for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
